import time
import pytest
import allure
import TestCase

from Base.InitDriver import init_driver
from Page.CommonPage import CommonPage
from Utils.XiaoMaUtils import XiaoMaUtils


@allure.feature('TestCase_08: 排行榜的专辑点击封面会重新播放')
@allure.issue(
    'https://www.tapd.cn/56767367/sparrow/tcase/view/1156767367001043368?url_cache_key=17e362af8b12add5e45c5e468a11c15a')
class TestCaseMusic08:
    """
    预期结果: 重新从该专辑的第一首进行播放
    """

    def setup_class(self):
        self.driver = init_driver()
        # 实例化页面
        self.launcher_index_page = CommonPage(self.driver).get_launcher_index_page()
        self.launcher_app_list_page = CommonPage(self.driver).get_launcher_app_list_page()

        self.music_choice_page = CommonPage(self.driver).get_music_choice_page()
        self.music_index_page = CommonPage(self.driver).get_music_index_page()

    def teardown_class(self):
        XiaoMaUtils.close_app('music')
        self.driver.quit()

    @allure.story("step1: 点击列表音乐的封面, 播放10分钟")
    @pytest.mark.run(order=81)
    def test_music_08_1_click_list_music_img(self):
        try:
            # 进入音乐
            self.launcher_index_page.click_app_list()
            self.launcher_app_list_page.click_appname(TestCase.MUSIC)
            if self.music_choice_page.is_display_title():
                self.music_choice_page.click_skip()

            self.music_index_page.click_rank()
            self.music_index_page.click_list_music(1)
            thumb_title1 = self.music_index_page.get_element_attribute(self.music_index_page.get_thumb_info(),
                                                                       TestCase.ELEMENT_TEXT)
            time.sleep(360)
            thumb_title2 = self.music_index_page.get_element_attribute(self.music_index_page.get_thumb_info(),
                                                                       TestCase.ELEMENT_TEXT)

            self.music_index_page.click_list_music(1)
            thumb_title3 = self.music_index_page.get_element_attribute(self.music_index_page.get_thumb_info(),
                                                                       TestCase.ELEMENT_TEXT)
            print(thumb_title1)
            print(thumb_title2)
            print(thumb_title3)
            assert (thumb_title1 != thumb_title2) and (thumb_title1 == thumb_title3)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)
            allure.attach(thumb_title1, '第一次点击获取的歌曲名', allure.attachment_type.TEXT)
            allure.attach(thumb_title2, '播放十分钟后的歌曲名', allure.attachment_type.TEXT)
            allure.attach(thumb_title3, '再次点击获取的歌曲名', allure.attachment_type.TEXT)


if __name__ == '__main__':
    # pytest.main("-s testcase_05.py")
    pass
